package com.baolai.service.impl;

import com.baolai.entity.Goods;
import com.baolai.mapper.GoodsMapper;
import com.baolai.result.Result;
import com.baolai.result.ResultUtil;
import com.baolai.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Override
    public Result addGoods(Goods goods) {
        Goods goods1 = this.getById(goods.getId());
        if(goods1 != null){
            return ResultUtil.error("该备件编号已存在,请更换编号");
        }
        if(this.save(goods)){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @Override
    public IPage<Goods> listGoods(Integer current, Integer size) {
        IPage<Goods> page = new Page<>(current,size);
        return this.page(page);
    }

    @Override
    public IPage<Goods> queryGoods(String name, String type, Integer current, Integer size) {
        IPage<Goods> page = new Page<>(current,size);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if(name.length() > 0){
            queryWrapper = queryWrapper.like("name",name);
        }
        if(type.length() > 0){
            queryWrapper = queryWrapper.like("type",type);
        }
        return this.page(page,queryWrapper);
    }

    @Override
    public boolean updateGoods(Goods goods) {
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",goods.getId());
        return this.update(goods,updateWrapper);
    }
}
